using System;
using System.Windows.Forms;
using Sixeyed.CodeGeneration.Metadata;
using Sixeyed.CodeGeneration.Metadata.Interfaces;
using Sixeyed.CodeGeneration.UI.Enums;

namespace Sixeyed.CodeGeneration.UI
{
    /// <summary>
    /// Control for selecting metadata source and provider
    /// </summary>
    public partial class SourceSelection : UserControl
    {
        public delegate void SourceSelectionChangedHandler(object sender, SourceSelectionType changeType);
        public event SourceSelectionChangedHandler SourceSelectionChanged;

        /// <summary>
        /// Default constructor
        /// </summary>
        public SourceSelection()
        {
            InitializeComponent();
            this.LoadSourceTypes();
        }

        #region Public properties

        /// <summary>
        /// Gets the selected metadata source type
        /// </summary>
        public string SelectedSourceType
        {
            get { return this.cboSourceType.SelectedItem.ToString(); }
        }

        /// <summary>
        /// Gets the selected metadata source
        /// </summary>
        public IMetadataSource SelectedSource
        {
            get
            {
                IMetadataSource source = null;
                if (this.cboSource.SelectedItem != null)
                {
                    source = SourceContainer.GetSource(this.cboSource.SelectedItem.ToString());
                }
                return source;
            }
        }

        /// <summary>
        /// Gets the selected metadata provider
        /// </summary>
        public IMetadataProvider SelectedProvider
        {
            get
            {
                IMetadataProvider provider = null;
                if (this.SelectedSource != null && this.cboProvider.SelectedItem != null)
                {
                    provider = this.SelectedSource.GetProvider(this.cboProvider.SelectedItem.ToString());
                }
                return provider;
            }
        }

        #endregion

        #region Public instance methods

        /// <summary>
        /// Returns whether the selected metadata source is fully configured
        /// </summary>
        /// <returns></returns>
        public bool IsConfigured()
        {
            return (this.SelectedSource != null &&
                    !string.IsNullOrEmpty(this.SelectedSource.Uri) &&
                    this.SelectedProvider != null);
        }

        /// <summary>
        /// Sets the selected metadata source type
        /// </summary>
        /// <param name="sourceTypeName">Source type name</param>
        public void SetSourceType(string sourceTypeName)
        {
            this.SetComboBoxValue(this.cboSourceType, sourceTypeName);
        }

        /// <summary>
        /// Sets the selected metdata source
        /// </summary>
        /// <param name="sourceName">Source name</param>
        public void SetSource(string sourceName)
        {
            this.SetComboBoxValue(this.cboSource, sourceName);
        }

        /// <summary>
        /// Sets the selected metadata source Uri
        /// </summary>
        /// <param name="value">Source Uri</param>
        public void SetSourceUri(string sourceUri)
        {
            if (!string.IsNullOrEmpty(sourceUri) && this.SelectedSource != null)
            {
                this.SelectedSource.Uri = sourceUri;
            }
        }

        /// <summary>
        /// Sets the selected metadata provider name
        /// </summary>
        /// <param name="providerName">Provider name</param>
        public void SetProvider(string providerName)
        {
            this.SetComboBoxValue(this.cboProvider, providerName);
        }

        #endregion

        #region Private instance methods

        /// <summary>
        /// Load known metadata source types
        /// </summary>
        private void LoadSourceTypes()
        {
            this.cboSourceType.Items.Clear();
            this.cboSource.Items.Clear();
            this.cboProvider.Items.Clear();
            this.btnSourceUri.Visible = false;
            if (SourceContainer.Loaded)
            {
                this.cboSourceType.Items.AddRange(SourceContainer.GetSourceTypeNames().ToArray());
            }
        }

        /// <summary>
        /// Load available metadata sources for the selected source type
        /// </summary>
        private void LoadSources()
        {
            this.cboSource.Items.Clear();
            this.cboProvider.Items.Clear();
            this.cboSource.Items.AddRange(SourceContainer.GetSourceNames(this.cboSourceType.SelectedItem.ToString()).ToArray());
            this.cboSource.Enabled = true;
            this.cboProvider.Enabled = false;
            this.btnSourceUri.Visible = true;
        }

        /// <summary>
        /// Load available metadata proividers for the selected source
        /// </summary>
        private void LoadProviders()
        {
            this.cboProvider.Items.Clear();
            this.cboProvider.Items.AddRange(this.SelectedSource.GetProviderNames().ToArray());
            this.cboProvider.Enabled = false;
        }

        private void SetComboBoxValue(ComboBox comboBox, string itemName)
        {
            if (!string.IsNullOrEmpty(itemName) && comboBox.Items.Contains(itemName))
            {
                comboBox.SelectedItem = itemName;
            }
        }

        private void cboSourceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadSources();
            this.NotifySelectionChange(SourceSelectionType.SourceType);
        }

        private void cboSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadProviders();
            this.NotifySelectionChange(SourceSelectionType.Source);
        }

        private void NotifySelectionChange(SourceSelectionType changeType)
        {
            if (this.SourceSelectionChanged != null)
            {
                this.SourceSelectionChanged(this, changeType);
            }
        }

        private void btnSourceUri_Click(object sender, EventArgs e)
        {
            SourceUri dialog = new SourceUri();
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.Source = this.SelectedSource;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.NotifySelectionChange(SourceSelectionType.SourceUri);
            }
            this.cboProvider.Enabled = !string.IsNullOrEmpty(this.SelectedSource.Uri);
        }

        private void cboProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NotifySelectionChange(SourceSelectionType.Provider);
        }

        #endregion
    }
}